﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.ComponentModel;
    using System.Xml;

    [ScriptType("SP.ContentTypeId", ValueObject=true, ServerTypeId="{da0f1e90-296f-480e-bc27-cefe51eff241}")]
    public sealed class ContentTypeId : ClientValueObject
    {
        private string m_stringValue;

        public override bool CustomFromJson(JsonReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (reader.PeekTokenType() != JsonTokenType.Null)
            {
                reader.ReadObjectStart();
                while (reader.PeekTokenType() != JsonTokenType.ObjectEnd)
                {
                    string peekedName = reader.PeekName();
                    if (!this.InitOnePropertyFromJson(peekedName, reader))
                    {
                        if (peekedName == "StringValue")
                        {
                            reader.ReadName();
                            this.m_stringValue = reader.ReadString();
                        }
                        else
                        {
                            reader.ReadName();
                            reader.ReadObject();
                        }
                    }
                }
                reader.ReadObjectEnd();
            }
            return true;
        }

        public override bool CustomWriteToXml(XmlWriter writer, SerializationContext serializationContext)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (serializationContext == null)
            {
                throw new ArgumentNullException("serializationContext");
            }
            writer.WriteStartElement("Property");
            writer.WriteAttributeString("Name", "StringValue");
            DataConvert.WriteValueToXmlElement(writer, this.m_stringValue, serializationContext);
            writer.WriteEndElement();
            return true;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            return false;
        }

        public override string ToString()
        {
            return this.m_stringValue;
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override void WriteToXml(XmlWriter writer, SerializationContext serializationContext)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (serializationContext == null)
            {
                throw new ArgumentNullException("serializationContext");
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string TypeId
        {
            get
            {
                return "{da0f1e90-296f-480e-bc27-cefe51eff241}";
            }
        }
    }
}

